package com.jianyun.jyzs.model;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.jianyun.jyzs.bean.OaAppBean;
import com.jianyun.jyzs.bean.OaCustomBean;
import com.jianyun.jyzs.bean.TopBrodCastBean;
import com.jianyun.jyzs.http.Api;
import com.jianyun.jyzs.http.CustomCallback;
import com.jianyun.jyzs.http.RetrofitHelper;
import com.jianyun.jyzs.model.imdoel.INewCustomModel;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangshijun on 2018/10/23.
 */

public class NewCustomModel implements INewCustomModel {

    private static NewCustomModel model;

    private NewCustomModel() {
    }

    public static NewCustomModel getInstance() {
        if (null == model) {
            model = new NewCustomModel();
        }
        return model;
    }

    @Override
    public void onGetServiceJson(String url, final OnGetCustomLisener onGetCustomLisener) {
        Api api = RetrofitHelper.builderNewSignHttp().create(Api.class);
        api.getCustomMain(url)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Object>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        onGetCustomLisener.onFailed();
                    }

                    @Override
                    public void onNext(Object o) {
                        Gson gson = new Gson();
                        String s = gson.toJson(o);
                        try {
                            //Log.i("test", "介些应用2：" + o.toString());
                            JsonParser parser = new JsonParser();
                            JsonArray jsonArray = parser.parse(s).getAsJsonArray();
                            List<OaAppBean> oaBeanList = new ArrayList<>();
                            for (JsonElement oabean :
                                    jsonArray) {
                                OaAppBean oaAppBean = gson.fromJson(oabean, OaAppBean.class);
                                oaBeanList.add(oaAppBean);
                            }
                            onGetCustomLisener.onSuccess2(oaBeanList);
                            //Log.i("test", "介些应用：" + oaBeanList.toString());
                        } catch (Exception e) {
                            try {
                                OaCustomBean oaCustomBean = gson.fromJson(s, OaCustomBean.class);
                                onGetCustomLisener.onSuccess(oaCustomBean);
                            }catch (Exception es){
                                Log.e("err", "应用解析错误");
                                es.printStackTrace();
                                onGetCustomLisener.onFailed();
                            }

                        }
                    }
                });
    }

    @Override
    public void getTopcaset(String Url, final OnGetTopCastListener onGetTopCastListener) {
        Api api = RetrofitHelper.builderTopImghttp().create(Api.class);
        api.getTopBrodCasetImg(Url)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<TopBrodCastBean>() {
                    @Override
                    protected void onResponse(TopBrodCastBean response) {
                        Log.i("test", "顶部论哦图：" + response.toString());
                        onGetTopCastListener.onGetTopImgSuccess(response);
                    }

                    @Override
                    protected void onException(String msg) {
                        Log.e("err", "轮播图错误：" + msg);
                    }
                });
    }
}
